package com.eqdd.yiqidian.ui.yiqixue;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.SearchView;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;

import com.eqdd.yiqidian.LearnSearchActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.ui.yiqixue.fragment.FindArticleFragment;
import com.eqdd.yiqidian.ui.yiqixue.fragment.FindLectureFragment;
import com.eqdd.yiqidian.ui.yiqixue.fragment.FindNeedFragment;
import com.eqdd.yiqidian.ui.yiqixue.fragment.FindTeacherFragment;
import com.eqdd.yiqidian.ui.yiqixue.fragment.FindVideoFragment;
import com.eqdd.yiqidian.utils.ClickUtil;

/**
 * @author吕志豪 .
 * @date 18-2-9  上午8:50.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class LearnSearchActivity extends BaseActivity {
    private LearnSearchActivityCustom dataBinding;
    private SearchView.SearchAutoComplete mSearchAutoComplete;
    private FindTeacherFragment findTeacherFragment;
    private FindLectureFragment findLectureFragment;
    private FindVideoFragment findVideoFragment;
    private FindArticleFragment findArticleFragment;
    private FindNeedFragment findNeedFragment;
    private int currentPos;
    private Bundle bundle;
    private String field;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_learn_search);
    }

    private void showFragment(int pos) {
        switch (pos) {
            case 0:
                showTeacherFragment();
                findTeacherFragment.refresh(HttpConfig.LEARN_TEACHER_SEARCH_LIST, dataBinding.searchView.getQuery().toString());
                break;
            case 1:
                showLectureFragment();
                findLectureFragment.refresh(HttpConfig.LEARN_LECTURE_SEARCH_LIST, dataBinding.searchView.getQuery().toString());
                break;
            case 2:
                showVideoFragment();
                findVideoFragment.refresh(HttpConfig.LEARN_VIDEO_SEARCH_LIST, dataBinding.searchView.getQuery().toString());
                break;
            case 3:
                showArticleFragment();
                findArticleFragment.refresh(HttpConfig.LEARN_ARTICLE_SEARCH_LIST, dataBinding.searchView.getQuery().toString());
                break;
            case 4:
                showNeedFragment();
                findNeedFragment.refresh(dataBinding.searchView.getQuery().toString());
                break;
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        currentPos = intent.getIntExtra(Config.POS, 0);
        field = intent.getStringExtra(Config.FIELD);
        showFragment(currentPos, field);
    }

    private void showFragment(int pos, String field) {
        switch (pos) {
            case 0:
                showTeacherFragment();
                // findTeacherFragment.filed(HttpConfig.LEARN_TEACHER_SEARCH_FILED_LIST, field);
                findTeacherFragment.filed(HttpConfig.LEARN_TEACHER_SEARCH_LIST, field);
                break;
            case 1:
                showLectureFragment();
                findLectureFragment.filed(HttpConfig.LEARN_LECTURE_SEARCH_FIELD_LIST, field);
                break;
            case 2:
                showVideoFragment();
                findVideoFragment.filed(HttpConfig.LEARN_VIDEO_SEARCH_FIELD_LIST, field);
                break;
            case 3:
                showArticleFragment();
                findArticleFragment.filed(HttpConfig.LEARN_ARTICLE_SEARCH_FILED_LIST, field);
                break;
            case 4:
                showNeedFragment();
                findNeedFragment.refresh(dataBinding.searchView.getQuery().toString());
                break;
        }
    }

    private void showTeacherFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (findTeacherFragment == null) {
            bundle = new Bundle();
            bundle.putString(Config.URL, HttpConfig.LEARN_TEACHER_SEARCH_LIST);
            findTeacherFragment = new FindTeacherFragment();
            findTeacherFragment.setArguments(bundle);
            fragmentTransaction.add(R.id.frameLayout, findTeacherFragment);
        }
        hideFragment(fragmentTransaction);
        fragmentTransaction.show(findTeacherFragment);
        fragmentTransaction.commitNow();
    }

    private void showLectureFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        if (findLectureFragment == null) {
            bundle = new Bundle();
            bundle.putString(Config.URL, HttpConfig.LEARN_LECTURE_SEARCH_LIST);
            findLectureFragment = new FindLectureFragment();
            findLectureFragment.setArguments(bundle);
            fragmentTransaction.add(R.id.frameLayout, findLectureFragment);
        }
        hideFragment(fragmentTransaction);
        fragmentTransaction.show(findLectureFragment);
        fragmentTransaction.commitNow();
    }

    private void showNeedFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (findNeedFragment == null) {
            bundle = new Bundle();
            bundle.putString(Config.URL, "Training/Trains/Get_TrainBySearch.ashx");
            findNeedFragment = new FindNeedFragment();
            findNeedFragment.setArguments(bundle);
            fragmentTransaction.add(R.id.frameLayout, findNeedFragment);
        }
        hideFragment(fragmentTransaction);
        fragmentTransaction.show(findNeedFragment);
        fragmentTransaction.commitNow();
    }

    private void showVideoFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (findVideoFragment == null) {
            bundle = new Bundle();
            findVideoFragment = new FindVideoFragment();
            findVideoFragment.setArguments(bundle);
            fragmentTransaction.add(R.id.frameLayout, findVideoFragment);
        }
        hideFragment(fragmentTransaction);
        fragmentTransaction.show(findVideoFragment);
        fragmentTransaction.commitNow();
    }

    private void showArticleFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (findArticleFragment == null) {
            bundle = new Bundle();
            findArticleFragment = new FindArticleFragment();
            findArticleFragment.setArguments(bundle);
            fragmentTransaction.add(R.id.frameLayout, findArticleFragment);
        }
        hideFragment(fragmentTransaction);
        fragmentTransaction.show(findArticleFragment);
        fragmentTransaction.commitNow();
    }

    private void hideFragment(FragmentTransaction fragmentTransaction) {
        if (findTeacherFragment != null) {
            fragmentTransaction.hide(findTeacherFragment);
        }
        if (findVideoFragment != null) {
            fragmentTransaction.hide(findVideoFragment);
        }
        if (findLectureFragment != null) {
            fragmentTransaction.hide(findLectureFragment);
        }
        if (findArticleFragment != null) {
            fragmentTransaction.hide(findArticleFragment);
        }
        if (findNeedFragment != null) {
            fragmentTransaction.hide(findNeedFragment);
        }
    }

    @Override
    public void initData() {
        currentPos = getIntent().getIntExtra(Config.POS, 0);
        field = getIntent().getStringExtra(Config.FIELD);
        dataBinding.searchView.setIconified(false);//设置searchView处于展开状态
        dataBinding.searchView.onActionViewExpanded();// 当展开无输入内容的时候，没有关闭的图标
        dataBinding.searchView.setQueryHint("请输入");//设置默认无内容时的文字提示
        dataBinding.searchView.requestFocus();
        mSearchAutoComplete = dataBinding.searchView.findViewById(R.id.search_src_text);

        //设置输入框提示文字样式
        mSearchAutoComplete.setHintTextColor(getResources().getColor(android.R.color.white));//设置提示文字颜色
        mSearchAutoComplete.setTextColor(getResources().getColor(android.R.color.white));//设置内容文字颜色


        dataBinding.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                LearnSearchActivity.this.currentPos = position;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        ClickUtil.click(dataBinding.ivBt, () -> {
            if (!TextUtils.isEmpty(dataBinding.searchView.getQuery())) {
                showFragment(this.currentPos);
            }
        });
        dataBinding.spinner.setSelection(currentPos, true);
        if (!TextUtils.isEmpty(field)) {
            showFragment(this.currentPos, field);
        }
    }

    @Override
    public void setView() {
        ClickUtil.click(dataBinding.back, this::onBackPressed);
    }
}
